package cn.gson.school.controller.jiaoxue;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.*;
import cn.gson.school.dao.jiaoxue.*;
import cn.gson.school.entity.jiaoxue.*;
import cn.gson.school.entity.xitong.*;
import cn.gson.school.mappers.jiaoxue.TiMuMappers;
import cn.gson.school.service.ImagesService;

@Controller
@RequestMapping("/timu")
public class TiMuController {

	@Autowired
	private TiMuDao tiMuDao;

	@Autowired
	private TiMuMappers tiMuMappers;
	
	@Autowired
	private ZhangJieDao zhangJieDao;
	
	@Autowired
	private ImagesService imgService;

	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> index(TitleEntity titleEntity,
			@RequestParam(name = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "rows", defaultValue = "20") Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<TitleEntity> list = tiMuMappers.tiMuList(titleEntity);
		PageInfo<TitleEntity> info = new PageInfo<>(list);
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	@RequestMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> save(@Valid TitleEntity titleEntity,HttpServletRequest request,Model model,BindingResult br,@SessionAttribute("user") UserEntity user,@RequestParam("file")MultipartFile file) {
		Map<String, Object> obj=new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("errors", br.getFieldErrors());
		}else {
			Long titleId=titleEntity.getTitleId();//获取题目id
			String userId=request.getParameter("userId");//获取当前操作人编号
			String phaseId=request.getParameter("phase");//获取阶段编号
			List<ChapterEntity> chapterEntity=zhangJieDao.findByPhaseId(Long.valueOf(phaseId));//获取章节
			String titleType=request.getParameter("timu");
			String titleName=request.getParameter("titleName");//题目
			TitleEntity title;
			if (titleId==null) {//添加
				title=new TitleEntity();
				title.setProblemSettingTime(new Date());
			}else {//编辑
				title=tiMuDao.findOne(titleId);
			}
			String answer = null;
			if (titleType.equals("选择题")) {
				title.setOptionA(titleEntity.getOptionA());
				title.setOptionB(titleEntity.getOptionB());
				title.setOptionC(titleEntity.getOptionC());
				title.setOptionD(titleEntity.getOptionD());
				String answerA=request.getParameter("answerA");
				answer=answerA;
			}else if(titleType.equals("判断题")){
				String answerB=request.getParameter("answerB");
				answer=answerB;
			}else if(titleType.equals("代码题")){
				String answerC=request.getParameter("answerC");
				answer=answerC;
			}
			ImageEntity img = null;
			if (file.getSize()!=0) {
				try {
					img = imgService.upload(user, "title", file,"");
				} catch (Exception e) {
					obj.put("上传失败！", e);
				}
			}

			System.out.println("答案："+answer);
			title.setTitleAnswer(answer);
			title.setTitle(titleName);
			title.setImageId(img);
			title.setTitleType(titleType);
			title.setChapterId(chapterEntity.get(0));
			title.setDelete(1);
			tiMuDao.save(title);
			obj.put("success", true);
		}
		return obj;
	}
	
	@RequestMapping("/delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		TitleEntity titleEntity=tiMuDao.findOne(id);
		if (id!=null&&titleEntity!=null) {
			titleEntity.setDelete(0);
			tiMuDao.save(titleEntity);
			obj.put("success", true);
		}else {
			obj.put("message", "你要删除的记录不存在！");
		}
		return obj;
	}
	
}
